package com.example.shortvideobackend.controller;

import com.example.shortvideobackend.entity.LiveStream;
import com.example.shortvideobackend.service.LiveStreamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/live")
@CrossOrigin(origins = "*")
public class LiveStreamController {

    @Autowired
    private LiveStreamService liveStreamService;

    // 创建直播流
    @PostMapping("/create")
    public ResponseEntity<?> createLiveStream(@RequestBody Map<String, String> request) {
        try {
            String title = request.get("title");
            String description = request.get("description");
            Long userId = Long.parseLong(request.get("userId"));

            LiveStream liveStream = liveStreamService.createLiveStream(title, description, userId);

            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", liveStream);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 开始直播
    @PostMapping("/{streamId}/start")
    public ResponseEntity<?> startLiveStream(@PathVariable Long streamId, @RequestParam Long userId) {
        try {
            LiveStream liveStream = liveStreamService.startLiveStream(streamId, userId);

            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", liveStream);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 结束直播
    @PostMapping("/{streamId}/end")
    public ResponseEntity<?> endLiveStream(@PathVariable Long streamId, @RequestParam Long userId) {
        try {
            LiveStream liveStream = liveStreamService.endLiveStream(streamId, userId);

            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", liveStream);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 获取所有直播中的流
    @GetMapping
    public ResponseEntity<?> getLiveStreams() {
        try {
            List<LiveStream> liveStreams = liveStreamService.getLiveStreams();

            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", liveStreams);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 根据ID获取直播流
    @GetMapping("/{streamId}")
    public ResponseEntity<?> getLiveStreamById(@PathVariable Long streamId) {
        try {
            LiveStream liveStream = liveStreamService.getLiveStreamById(streamId);
            // 增加观看人数
            liveStreamService.incrementViewers(streamId);

            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", liveStream);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 根据streamKey获取直播流
    @GetMapping("/key/{streamKey}")
    public ResponseEntity<?> getLiveStreamByKey(@PathVariable String streamKey) {
        try {
            LiveStream liveStream = liveStreamService.getLiveStreamByKey(streamKey);

            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", liveStream);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 获取用户的直播流
    @GetMapping("/user/{userId}")
    public ResponseEntity<?> getUserLiveStreams(@PathVariable Long userId) {
        try {
            List<LiveStream> liveStreams = liveStreamService.getUserLiveStreams(userId);

            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", liveStreams);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 点赞直播
    @PostMapping("/{streamId}/like")
    public ResponseEntity<?> likeLiveStream(@PathVariable Long streamId) {
        try {
            liveStreamService.incrementLikes(streamId);

            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "点赞成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 删除直播流
    @DeleteMapping("/{streamId}")
    public ResponseEntity<?> deleteLiveStream(@PathVariable Long streamId, @RequestParam Long userId) {
        try {
            liveStreamService.deleteLiveStream(streamId, userId);

            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "删除成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
} 